package com.jie.tcp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端
 */
public class TcpSever {
    public static void main(String[] args) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        InputStream inputStream = null;
        Socket socket = null;
        ServerSocket serverSocket= null;
        try {
            //设置地址
            serverSocket= new ServerSocket(9999);
            socket = serverSocket.accept();
            inputStream = socket.getInputStream();
            byte[] buffer = new byte[1024];
            int len;
            byteArrayOutputStream = new ByteArrayOutputStream();
            while((len = inputStream.read(buffer))!= -1){
                byteArrayOutputStream.write(buffer,0,len);
                System.out.println(new String(buffer,0,len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                byteArrayOutputStream.close();
                inputStream.close();
                socket.close();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
